package cn.com.pansky.action.person.contact;

import java.util.ArrayList;
import java.util.List;

import net.vsame.url2sql.action.CommonAction;
import net.vsame.url2sql.helper.SqlHelper;
import net.vsame.url2sql.sql.Model;
import net.vsame.url2sql.utils.PostParam;
import cn.com.pansky.helper.PostHelper;
import cn.com.pansky.sdk.API;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class ContactAction extends CommonAction{

	/**
	 * 获取联系人列表
	 */
	public void list() {
		String sql = urlConfig.getVar("list");
		List<Model> list = SqlHelper.query(sql, user.getString("AA032"), user.getString("AA003"));
		
		String CA006 = user.getString("CA006");
		for (int i = 0; i < list.size(); i++) {
			Model model = list.get(i);
			if (CA006.equals(model.getString("CA006"))) {
				model.remove("CA002");
				model.add("CA002", "1");
			} else {
				model.remove("CA002");
				model.add("CA002", "0");
			}
		}
		
		
		String[] AAC002 = new String[list.size()];
		for (int i=0; i<list.size(); i++) {
			Model model = list.get(i);
			AAC002[i] = model .getString("CA006");
			//model.put("CA006", model.getString("CA006").replace(model.getString("CA006").substring(1, 17), "****************"));
		}
		JSONArray jsonArray = API.getPersonState(AAC002); //请求业务系统接口
		List<Model> retList = new ArrayList<Model>();
		if(jsonArray.size()>0) {
			//重组数据
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject obj = (JSONObject)jsonArray.get(i);
				String aac002 = obj.getString("AAC002");
				JSONObject json = obj.getJSONObject("ITEM");
				for (Model model : list) {
					if(aac002.equals(model.getString("CA006"))) {
						isRetired(model);
						
						
						//判断业务库返回的联系电话和地址是否空，如果为空给前台返回本地数据库中保存的联系电话和地址
//						String AAE005 = json.getString("AAE005");
//						AAE005 = AAE005.equals("") ? "" : AAE005;
//						String AAE006 = json.getString("AAE006");
						
						model.put("CA006", model.getString("CA006").replace(model.getString("CA006").substring(1, 17), "****************"));
						model.put("AAC084", json.getString("AAC084")); // 享受待遇
						model.put("AAC031", json.getString("AAC031")); // 缴费状态
						model.put("AAE0160", json.getString("AAE0160")); // 参保状态
						model.put("MINYEAR", json.getString("MINYEAR"));
						model.put("AAE0161", json.getString("AAE0161")); // 待遇申请
						model.put("AAE0162", json.getString("AAE0162")); // 转移申请
						model.put("ISDAOLIN", json.getString("ISDAOLIN")); // 到龄标志
						model.put("MAXYEAR", json.getString("MAXYEAR"));
						model.put("AAE005", json.getString("AAE005"));//联系电话
						model.put("AAE006", json.getString("AAE006"));//地址
						
						retList.add(model);
					}
				}
			}
			context.getDatas().put("result", retList);
		} else {
			for (int i=0; i<list.size(); i++) {
				Model model = list.get(i);
				model.put("CA006", model.getString("CA006").replace(model.getString("CA006").substring(1, 17), "****************"));
			}
			context.getDatas().put("result", list);
		}
	}
	
	/**
	 * 获取人员是否是领取待遇人员，是否认证通过
	 * @param model
	 * @param CA003
	 * @param CA006
	 */
	private void isRetired(Model model) {
		List<PostParam> p = new ArrayList<PostParam>();
		PostParam AAC002 = new PostParam("AAC002", "string", model.getString("CA006"));// 身份证号
		PostParam AAC003 = new PostParam("AAC003", "string", model.getString("CA003"));// 姓名
		p.add(AAC002);
		p.add(AAC003);
		JSONObject results = PostHelper.send("auth/isNeedAuth.json", p);
		boolean isRetired = (Boolean) results.get("isRetired");
		boolean isAuthed = (Boolean) results.get("isAuthed");

		model.add("isRetired", isRetired ? 1 : 0);
		model.add("isAuthed", isAuthed ? 1 : 0);
	}
}
